// 创建节点
function ListNode(val, next) {
  this.val = val === undefined ? 0 : val;
  this.next = val === undefined ? null : next;
}

// 数组转链表
function createList(arr) {
  let dummy = new ListNode(0);
  let p = dummy;
  for (let i = 0; i < arr.length; i++) {
    p.next = new ListNode(arr[i]);
    p = p.next;
  }
  return dummy.next;
}

function listToArr(list) {
  const arr = [];
  while (list) {
    arr.push(list.val);
    list = list.next;
  }
  return arr;
}

let list = createList([1, 2, 3, 4])
// console.log(list);
console.log(listToArr(list));